package com.zprmb.udzt;



import java.text.DecimalFormat;
import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.view.LineView;
import com.zprmb.udzt.view.WaveHelper;
import com.zprmb.udzt.view.WaveView;
/**
 * 我的智投宝
 * @author shenshao
 *
 */
public class V6MineDexterityActivity extends BaseActivity implements OnClickListener{

	
	
	private WaveView wv;
	private WaveHelper helper;
	private TextView tv_yesterday,tv_my_investnum,tv_remain,tv_available_Balance,tv_jieshao,tv_up_money;
	private ImageView img_back;
	private Button btn_in,btn_out;
	private RelativeLayout rl_shouyi,rl_detail;
	private ScrollView sv;
	private String id,url,desc;
	private Double million;
	private View img_rect;
	private LinearLayout li_btn;
	private TextView tv_desc,avgAmountThisMonth,tv_more;
	private DecimalFormat df = new DecimalFormat("###0.00");
	private ImageView img_nowork,img_list_none,img_help;
	private boolean login;
	private RelativeLayout rl_back;
	private LinearLayout li_shouyi,li_in,li_out;
	LineView lineView;
	private RelativeLayout rl_activity;
	
	
	private ArrayList<String> listdata=new ArrayList<String>();
	private ArrayList<Double> listamount=new ArrayList<Double>();
	private ArrayList<Double> listamount2=new ArrayList<Double>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lingcun_layout);
		 BbtApplication.getInstance().addActvity(this);
		setImmersionStatus();
		
		wv=(WaveView)findViewById(R.id.lingcun_wv);
		helper=new WaveHelper(wv);
		helper.start();
		
		initview();
		initData(); 
		zouni();
	}
	public void zouni() {
		
//		lineView.setShowPopup(LineView.SHOW_POPUPS_MAXMIN_ONLY); // optional
//		ArrayList<ArrayList<Integer>> dataLists=new ArrayList<ArrayList<Integer>>();
		
		ArrayList<String> strList = new ArrayList<String>();
		for (int i = 0; i < 7; i++) {
			strList.add((i+1)+"");
		}
		lineView.setBottomTextList(strList);
		//第一根线
		ArrayList<Double> dataList = new ArrayList<Double>();
		int random = (int) (Math.random() * 100 + 1);
		for (int i = 0; i < 7; i++) {
			dataList.add(10000.00+i);
		}
//		dataList.clear();
//		dataLists.add(dataList);
		//第二根线
//		ArrayList<Integer> dataList1 = new ArrayList<Integer>();
//		for (int i = 0; i < 20; i++) {
//			dataList1.add((int) (Math.random() * random));
//		}
//		dataLists.add(dataList1);
		
		lineView.setDataList(dataList);
	}
	private void initview() {
		// TODO Auto-generated method stub
	    lineView = (LineView) findViewById(R.id.line_view);
		tv_yesterday=(TextView)findViewById(R.id.yesterday_ac);
		tv_my_investnum=(TextView)findViewById(R.id.my_lingcun);
		li_in=(LinearLayout)findViewById(R.id.li_de_in);
		li_out=(LinearLayout)findViewById(R.id.li_de_out);
		sv=(ScrollView)findViewById(R.id.sv);
		tv_up_money=(TextView)findViewById(R.id.up_money);
		li_shouyi=(LinearLayout)findViewById(R.id.li_shouyi);
		tv_jieshao=(TextView)findViewById(R.id.tv_jieshao);
		img_back=(ImageView)findViewById(R.id.img_back);
		li_btn=(LinearLayout)findViewById(R.id.li_btn);
		img_help=(ImageView)findViewById(R.id.img_help);
		img_nowork=(ImageView)findViewById(R.id.iv_v4_nonetwork);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		img_rect=(View)findViewById(R.id.img_rect);
		tv_desc=(TextView)findViewById(R.id.tv_desc);
		rl_activity=(RelativeLayout)findViewById(R.id.rl_activity);
		avgAmountThisMonth=(TextView)findViewById(R.id.avgAmountThisMonth);
		img_list_none=(ImageView)findViewById(R.id.img_list_none);
		tv_more=(TextView)findViewById(R.id.tv_more);
		
		lineView.setShowYCoordinate(true);
		tv_more.setOnClickListener(this);
		rl_activity.setOnClickListener(this);
		img_help.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		tv_up_money.setOnClickListener(this);
		img_back.setOnClickListener(this);
		tv_jieshao.setOnClickListener(this);
		li_in.setOnClickListener(this);
		li_out.setOnClickListener(this);
		sv.setVisibility(View.GONE);
		li_btn.setVisibility(View.GONE);
		
		img_nowork.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				initData();
				islogin();
			}
		});
		
		tv_remain=(TextView)findViewById(R.id.Surplus_money);
		tv_available_Balance=(TextView)findViewById(R.id.available_Balance);
		btn_in=(Button)findViewById(R.id.recharge);
		btn_out=(Button)findViewById(R.id.withdraw);
        li_shouyi.setOnClickListener(this);
		btn_in.setOnClickListener(this);
		btn_out.setOnClickListener(this);
	
	islogin();
	}
	
	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				LogUtils.e("lingcun", jsonObject.toString());
				if(jsonObject.optInt("success")==1){					
					Gson gson=new Gson();
					sv.setVisibility(View.VISIBLE);
					li_btn.setVisibility(View.VISIBLE);
					img_nowork.setVisibility(View.GONE);
					JSONObject json1 = jsonObject.optJSONObject("data");
					JSONObject json2=json1.optJSONObject("userCurrentInfo");
					JSONObject json3=json1.optJSONObject("nearly7daysAmount");
					JSONObject json4=json1.optJSONObject("activityDesc"); 
					if(json3!=null){
						
						listdata=gson.fromJson(json3.optString("date"), new TypeToken<ArrayList<String>>() {
						}.getType());
						listamount=gson.fromJson(json3.optString("amount"), new TypeToken<ArrayList<Double>>() {
						}.getType());
						avgAmountThisMonth.setText("当月日平均余额(元)  : "+df.format(json1.optDouble("avgAmountThisMonth")));
					}
					
				if(listamount.size()>0){
					img_rect.setVisibility(View.VISIBLE);
						lineView.setVisibility(View.VISIBLE);
						lineView.setBottomTextList(listdata);
						lineView.setDataList(listamount);
						img_list_none.setVisibility(View.GONE);
						avgAmountThisMonth.setVisibility(View.VISIBLE);
					}else{
						img_rect.setVisibility(View.GONE);
						lineView.setVisibility(View.GONE);
						avgAmountThisMonth.setVisibility(View.GONE);
					}
				    url=json4.optString("url");
					tv_desc.setText(json4.optString("desc"));
					desc=json4.optString("desc");
					if(desc.length()<=0){
						rl_activity.setVisibility(View.INVISIBLE);
					}else{
						rl_activity.setVisibility(View.VISIBLE);
					}
//					Iterator it = json3.keys();  
//					List<String> keyListstr = new ArrayList<String>();  
//					List<String> valueListstr = new ArrayList<String>();  
//					while(it.hasNext()){  
//						keyListstr.add(it.next().toString());  
//						
//					}  
//					Log.e("key", keyListstr.toString());
//					for(int i = 0;i<keyListstr.size();i++){
//						valueListstr.add(json3.optString(keyListstr.get(i)));
//					}0.0
//					Log.e("value", valueListstr.toString());	 
					id=json1.optString("productId");
					million=json1.optDouble("millionAccrual");
					
					tv_remain.setText(df.format(Double.parseDouble(json1.optString("remainAmount")))+"元");
					if(json2==null){
						tv_my_investnum.setText(0.00+"");
						tv_yesterday.setText(0.00+"");
						tv_available_Balance.setText(0.00+"");
					}else{
						tv_my_investnum.setText(df.format(Double.parseDouble(json2.optString("amount"))));
						tv_yesterday.setText(df.format(Double.parseDouble(json2.optString("yesterdayProfit"))));
						tv_available_Balance.setText(df.format(Double.parseDouble(json2.optString("profit"))));
					}
				}else {
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(V6MineDexterityActivity.this,
								LoginActivity.class);
						intent.putExtra("close", 1);
						startActivity(intent);
					} else{
						Toast.makeText(V6MineDexterityActivity.this, jsonObject.optString("comment"), 2000).show();
					}
				}
				
				
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				sv.setVisibility(View.GONE);
				img_nowork.setVisibility(View.VISIBLE);
				li_btn.setVisibility(View.VISIBLE);
				Toast.makeText(V6MineDexterityActivity.this, "请求失败，请检查网络设置", 2000).show();
			}
		};
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true); 
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.execute(V6MineDexterityActivity.this,Constant.URL+"/api/v5/product/currentDetail", "");
		
	}
	private void islogin(){
		VolleryUtil volleryUtil=new VolleryUtil() {
					
					@Override
					public void onUpDate(JSONObject jsonObject) {
						// TODO Auto-generated method stub
						if (jsonObject.optInt("success")==1) {
							login=true;
						}else {
							if (jsonObject.optString("comment").equals("please login")) {
								Intent intent = new Intent(V6MineDexterityActivity.this,
										LoginActivity.class);
								intent.putExtra("close", 1);
								startActivity(intent);
							} else {
								ToastUtil.showCenterToast(V6MineDexterityActivity.this, jsonObject.optString("comment"));
							}
						}
						
					}
					
					@Override
					public void onError() {
						// TODO Auto-generated method stub
						
					}
				};
				volleryUtil.setHeaders("token", getToken());
				volleryUtil.setRequestMethod(0);
				volleryUtil.execute(V6MineDexterityActivity.this, Constant.URL+"apiAccount/userInfo", "");
			}
	private void setImmersionStatus() {
		if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
			// 透明状态栏
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		helper.cancel();
		
			BbtApplication.getInstance().removeActvity(this);
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent=null;
		switch(v.getId()){
		
		case R.id.img_back:
			finish();
			break;
		case R.id.recharge:
			if(!login){
				Toast.makeText(V6MineDexterityActivity.this, "请先登录", 2000).show();
				return;
			}
	     intent=new Intent(V6MineDexterityActivity.this,DexterityPayInActivity.class);
	     intent.putExtra("id", id);
	     intent.putExtra("million", million);
			startActivity(intent);
			break;
		case R.id.withdraw:
			if(!login){
				Toast.makeText(V6MineDexterityActivity.this, "请先登录", 2000).show();
				return;
			}
		     intent=new Intent(V6MineDexterityActivity.this,DexterityPayOutActivity.class);
		     intent.putExtra("id", id);
				startActivity(intent);
				break;
		case R.id.li_shouyi:
			if(!login){
				Toast.makeText(V6MineDexterityActivity.this, "请先登录", 2000).show();
				return;
			}
			intent=new Intent(V6MineDexterityActivity.this,DexterityShouyiActivity.class);
		     
				startActivity(intent);
			break;
		
		case R.id.rl_back:
			finish();
			break;
		case R.id.up_money:
			Intent intent3=new Intent(V6MineDexterityActivity.this,V4WebViewActivity.class);
	    	intent3.putExtra("url", "/h5/currCompareFixed?app=true");
	    	intent3.putExtra("title", "定期理财");
	    	
    	    startActivity(intent3);
			break;
		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		case R.id.tv_jieshao:
			Intent intent2=new Intent(V6MineDexterityActivity.this,V4WebViewActivity.class);
			intent2.putExtra("url", "/h5/knowCurrent?app=true");
			intent2.putExtra("title", "项目介绍");
			startActivity(intent2);
			break;
		case R.id.li_de_in:
			Intent intent5=new Intent(V6MineDexterityActivity.this,DexterityBillIn.class);
			
			startActivity(intent5);
			break;
		case R.id.li_de_out:
       Intent intent6=new Intent(V6MineDexterityActivity.this,DexterityBillOut.class);
			
			startActivity(intent6);
			break;
		case R.id.rl_activity:
		     Intent intent7=new Intent(V6MineDexterityActivity.this,V4WebViewActivity.class);
					intent7.putExtra("url", url+"?app=true");
					intent7.putExtra("title", desc);
			startActivity(intent7);
					break;
		case R.id.img_help:
			Intent intent8=new Intent(V6MineDexterityActivity.this,V4WebViewActivity.class);
			intent8.putExtra("url", "/h5/helpCurrent?app=true");
			intent8.putExtra("title", "项目帮助");
			startActivity(intent8);
			break;
		case R.id.tv_more:
			if(!login){
				Toast.makeText(V6MineDexterityActivity.this, "请先登录", 2000).show();
				return;
			}
			intent=new Intent(V6MineDexterityActivity.this,DexterityShouyiActivity.class);
		     
				startActivity(intent);
		break;
		}
		
	}

}
